package com.wh.minihelp.flower.dao;

import static com.wh.minihelp.flower.domain.SysFlower.ADD;
import static com.wh.minihelp.flower.domain.SysFlower.SUB;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wh.db.Page;
import com.wh.db.jdbc.mysql.BaseJDBCDao;
import com.wh.db.sql.SQLUtils;
import com.wh.lang.LogicException;
import com.wh.minihelp.flower.dao.FlowerDBMetas.ChildFlowerMapper;
import com.wh.minihelp.flower.dao.FlowerDBMetas.ChildFlowerSetter;
import com.wh.minihelp.flower.dao.FlowerDBMetas.FamilyFlowerMapper;
import com.wh.minihelp.flower.dao.FlowerDBMetas.FamilyFlowerSetter;
import com.wh.minihelp.flower.dao.FlowerDBMetas.FamilyFlowerUpdateSetter;
import com.wh.minihelp.flower.dao.FlowerDBMetas.NewChildFlowerMapper;
import com.wh.minihelp.flower.domain.ChildFlower;
import com.wh.minihelp.flower.domain.FamilyFlower;
import com.wh.minihelp.flower.domain.SysFlower;
import com.wh.minihelp.user.dao.UserDBMetas.ChildFlowerNumberUpdateSetter;
import com.wh.minihelp.user.dao.UserDBMetas.FamilyChildMapper;
import com.wh.minihelp.user.dao.UserDao;
import com.wh.minihelp.user.domain.AccountInfo;
import com.wh.minihelp.user.domain.ChildInfo;
import com.wh.minihelp.user.domain.FamilyAccount;
import com.wh.minihelp.user.util.UserUtil;

public class FlowerDao extends BaseJDBCDao {

	public static final String //
	TN_ChildFLower = "wh_flowermgr_child_flower";

	private SysFlower[] SysDefaultFlowers = { //
			new SysFlower(UserUtil.genUID(), "班级奖卡", ADD), //
			new SysFlower(UserUtil.genUID(), "课外班奖卡", ADD), //
			new SysFlower(UserUtil.genUID(), "家务分担", ADD), //
			new SysFlower(UserUtil.genUID(), "帮助他人", ADD), //
			new SysFlower(UserUtil.genUID(), "早睡加分", ADD), //
			new SysFlower(UserUtil.genUID(), "考试加分", ADD), //
			new SysFlower(UserUtil.genUID(), "才艺训练", ADD), //

			new SysFlower(UserUtil.genUID(), "兑换零用钱", SUB), //
			new SysFlower(UserUtil.genUID(), "发脾气", SUB), //
			new SysFlower(UserUtil.genUID(), "晚睡减分", SUB), //
			new SysFlower(UserUtil.genUID(), "考试减分", SUB), //
			new SysFlower(UserUtil.genUID(), "打架", SUB),//
	};

	public List<FamilyFlower> getChildFlowerTypes(FamilyAccount fa) {
		FamilyFlowerMapper mapper = new FamilyFlowerMapper();
		try {
			List<FamilyFlower> familyFlowers = ds.query(FamilyFlowerMapper.sql_all + " where t_familyId='" + fa.getFamilyId() + "'" + FamilyFlowerMapper.sql_order, mapper);
			if (familyFlowers.size() == 0) {
				FamilyFlowerSetter ffs = new FamilyFlowerSetter();
				String familyId = fa.getFamilyId();
				int size = SysDefaultFlowers.length;
				for (int i = 0; i < size; i++) {
					SysFlower sysFlower = SysDefaultFlowers[i];
					FamilyFlower ff = new FamilyFlower();
					ff.setId(UserUtil.genUID());
					ff.setCreateDate(new Date());
					ff.setFamilyId(familyId);
					ff.setName("￥" + sysFlower.getName());
					ff.setType(sysFlower.getType());
					ff.setOrder(i);
					ds.save(FamilyFlowerSetter.SQL_INSERT, ff, ffs);// 小孩
					familyFlowers.add(ff);
				}
				ds.saveOrUpdateAll();// 保存数据
			}
			return familyFlowers;
		} catch (Throwable e) {
			throw new LogicException("查询家庭红花类别信息失败。", e);
		}
	}

	// 保存家庭红花类别
	public void createFamilyFlower(FamilyFlower ff) {
		try {
			ds.save(FamilyFlowerSetter.SQL_INSERT, ff, new FamilyFlowerSetter());
			ds.saveOrUpdateAll();
		} catch (Throwable e) {
			throw new LogicException("保存家庭红花类别信息失败。", e);
		}
	}

	// 根据家庭红花类别的id获取对象
	public FamilyFlower getFamilyFlower(String id) {
		try {
			String sql = FamilyFlowerMapper.sql_all + " where id='" + SQLUtils.delSpecialSQLChar(id) + "'";
			return ds.queryForObject(sql, new FamilyFlowerMapper());
		} catch (Throwable e) {
			throw new LogicException("获取家庭红花类别信息失败。", e);
		}
	}

	// 修改家庭红花类别的顺序
	public void changeFamilyFlowerOrder(FamilyFlower f1, FamilyFlower f2) {
		try {
			Collection<FamilyFlower> ffs = new ArrayList<FamilyFlower>(2);
			ffs.add(f1);
			ffs.add(f2);
			ds.batchUpdate(FamilyFlowerUpdateSetter.SQL_UPDATE_ORDER, new FamilyFlowerUpdateSetter(), ffs, 2);
			ds.saveOrUpdateAll();
		} catch (Throwable e) {
			throw new LogicException("保存家庭红花类别信息失败。", e);
		}
	}

	// 删除某个家庭红花类别
	public void deleteFamilyFlower(String id) {
		String sql = "delete from " + FlowerDBMetas.TN_FamilyFlower + " where id='" + SQLUtils.delSpecialSQLChar(id) + "'";
		try {
			ds.execute(sql);
			ds.saveOrUpdateAll();
		} catch (Throwable e) {
			throw new LogicException("保存家庭红花类别信息失败。", e);
		}
	}

	// 删除某个家庭红花类别中加/减的最大顺序号
	public int getMaxFamilyFlowerOrder(String familyId, int type) {
		String sql = "select max(t_order) from " + FlowerDBMetas.TN_FamilyFlower //
				+ " where t_type=" + type //
				+ " and t_familyId='" + SQLUtils.delSpecialSQLChar(familyId) + "'"//
				+ " order by t_order desc";
		try {
			Number number = ds.queryForObject(sql);
			return number == null ? 0 : number.intValue();
		} catch (Throwable e) {
			throw new LogicException("获取家庭红花类别信息失败。", e);
		}
	}

	// 得到某小孩的红花列表
	public Page<ChildFlower> getChildFlowers(String childId, int start, int count) {
		ChildFlowerMapper mapper = new ChildFlowerMapper();
		Page<ChildFlower> page = getPage(
				ChildFlowerMapper.sql_all //
						+ " where t_childId='" + SQLUtils.delSpecialSQLChar(childId) + "'" + ChildFlowerMapper.sql_order//
				, start, count, mapper);
		fillAccountInfo(page.getList());
		return page;
	}

	private void fillAccountInfo(List<ChildFlower> flowers) {
		Map<String, AccountInfo> accounts = new HashMap<String, AccountInfo>();
		UserDao dao = ctx.getDao(UserDao.class);
		for (ChildFlower flower : flowers) {
			String account = flower.getOperAcount();
			AccountInfo acc = accounts.get(account);
			if (acc == null) {
				acc = dao.getAccountByWX(account);
				accounts.put(account, acc);
			}
			flower.setOperAcountInfo(acc);
		}
	}

	public List<ChildFlower> getNewsFlowers(String familyId) {
		NewChildFlowerMapper mapper = new NewChildFlowerMapper();
		List<ChildFlower> flowers = this.query(NewChildFlowerMapper.sql_all //
				+ " and ci.t_familyId='" + SQLUtils.delSpecialSQLChar(familyId) + "'"//
				+ NewChildFlowerMapper.sql_order//
				+ " limit 10;", mapper);
		fillAccountInfo(flowers);
		return flowers;
	}

	public ChildInfo saveChildFlower(ChildFlower cf) {
		// 保存此记录
		// 小孩记录上进行红花增减操作
		FamilyChildMapper mapper = new FamilyChildMapper();//
		try {
			ChildInfo d = ds.queryForObject(FamilyChildMapper.sql_all //
					+ " where id='" + cf.getChildId() + "'", mapper);
			if (d == null) throw new LogicException("无法找到对应的小孩信息。");
			int fn = cf.getFlowerNumber();
			if (fn < 0) {
				d.setUsed(d.getUsed() + fn * -1);// 记录用掉了多少
			} else {
				d.setTotals(d.getTotals() + fn);// 只记录增加的吧
			}
			ds.save(ChildFlowerNumberUpdateSetter.SQL_UPDATE_NAME, d, new ChildFlowerNumberUpdateSetter());// 小孩
			ds.save(ChildFlowerSetter.SQL_INSERT, cf, new ChildFlowerSetter());
			ds.saveOrUpdateAll();
			d.setRecordCount(d.getRecordCount() + 1);// 记录次数在保存红花时会自动加一
			return d;
		} catch (Throwable e) {
			throw new LogicException("添加小孩红花记录失败", e);
		}
	}
}